#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Kailash Budhathoki (kailash.buki@gmail.com)
"""Reads the translated code table file and stats file of the synthetic dataset
and generates the stats we need.

#exact_match = how many found patterns were found exactly the way we planted?
#intersection = how many found patterns are the intersection of the planted patterns?
#union = how many found patterns are the union of the planted patterns?
#others = how many found patterns consisted of union of planted alphabets with non planetd alphabets OR just non planted alphabets only?

usage: ./synth_stat.py <translated_ct_file> <synth_stats_file>
"""
import sys

ct_file = sys.argv[1]
stats_file = sys.argv[2]

common_patterns = []
splits = dict()
with open(stats_file, 'r') as statsfp:
    patterns = []
    split_id = 0
    for line in statsfp:
        if 'SPLIT' in line and patterns:
            splits[split_id] = patterns
            split_id += 1
            patterns = []
        elif '[' in line:
            cell = line.split('|')[0].strip()
            cell = cell.replace('[', '')
            cell = cell.replace(']', '')
            cell = cell.replace('*', '')
            itemset = set([int(item) for item in cell.split(',')])
            patterns.append(itemset)
            if '*' in line and itemset not in common_patterns:
                common_patterns.append(itemset)
        splits[split_id] = patterns

def get_intersection(split):
    intersections = []
    for i in xrange(len(split)):
        for j in xrange(i+1, len(split)):
            intersection = split[i].intersection(split[j])
            intersections.append(intersection)
    return intersections

def get_union(split):
    result = set()
    for pattern in split:
        result = result.union(pattern)
    return result

num_exact_matches = 0
num_intersections = 0
num_unions = 0
num_others = 0
with open(ct_file, 'r') as ctfp:
    cur_split = 0
    for line in ctfp:
        if 'CT' in line:
            if 'CT_Common' in line:
                cur_split += 1
            else:
                cur_split = int(line.split('::')[0].strip().split('_')[-1])
            if cur_split != len(splits):
                split = splits[cur_split]
            else:
                split = common_patterns
            intersections = get_intersection(split)
            union = get_union(split)
        elif line[0].isdigit():
            itemset = set([int(item) for item in line.split('\t')[0].strip().split()])
            print "%s\t" % str(itemset).rjust(100),
            if itemset in split:
                print "EXACT"
                num_exact_matches += 1
            elif itemset in intersections:
                print "INTER"
                num_intersections += 1
            elif itemset.issubset(union):
                print "UNION"
                num_unions += 1
            else:
                print "OTHERS"
                num_others += 1

print
print "-" * 50
print "#exact_match  = ", num_exact_matches
print "#intersection = ", num_intersections
print "#union        = ", num_unions
print "#others       = ", num_others
print "-" * 50
